<?php

declare(strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Business\v2;

use App\Dao\BusinessDao;
use App\Dao\CouponCategoryDao;
use App\Dao\CouponDao;
use App\Dao\CouponStockDao;
use App\Job\IntegralJob;
use App\Job\WriteBusinessMoneyJob;
use App\Kernel\Util\UserJwtInstance;
use App\Service\Service;
use App\Dao\UserCouponDao;
use App\Dao\UserDao;
use App\Middleware\Business\BusinessMiddleware;

use App\Service\User\IntegralDetailService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 优惠券服务
 *
 * @Middleware(BusinessMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Business
 */
class CouponService extends Service
{
    /**
     * @Inject
     * @var CouponDao
     */
    public $dao;

    /**
     * 添加全店打折商品
     * @param array $params
     */
    public function addShopDiscount(array $params)
    {
        $user = UserJwtInstance::instance()->build()->getUser();

        $business = $user->business;

        !$business && $this->error('BUSINESS.NOT_EXIST');

        if( $business['status'] != 2 && $business['status'] != 1 ) {
            $this->error('BUSINESS.NOT_PASS_CERT');
        }

        $data = [
            'business_id' => $business->id,
            'category_id' => 0,//暂时默认为0
            'type' => $this->dao::SHOP_DISCOUNT,//全店打折
            'title' => $params['title'],
            'picture' => $params['picture'],
            'status' => 0,
            'description' => '',//描述
        ];

        $result = !empty($params['id']) ? $this->dao->update([
            'id' => $params['id']
        ], $data) : $this->dao->create($data);

        !$result && $this->error();
    }

    /**
     * 添加优惠券
     *
     * @param array $params
     */
    public function addCoupon(array $params)
    {
        $user = UserJwtInstance::instance()->build()->getUser();

        $business = $user->business;

        !$business && $this->error('BUSINESS.NOT_EXIST');

        if( $business['status'] != 2 && $business['status'] != 1 ) {
            $this->error('BUSINESS.NOT_PASS_CERT');
        }

        // 优惠券开始时间 取时间戳
        $start_time  = strtotime($params['start_time']);

        // 固定截止日期 取时间戳
        $expire_time = strtotime($params['expire_time']);

        $data = [
            'business_id' => $business->id,
            'category_id' => 0,
            'type' => $this->dao::COUPON,
            'title' => $params['title'],
            'description' => $params['description'],
            'price' => $params['price'],
            'goods_price' => $params['goods_price'],
            'picture' => $params['picture'] ?? null,
            'start_time' => $start_time,
            'expire_time' => $expire_time,
            'status' => 0,
            'stock' => $params['stock'],
            'instructions' => $params['instructions'],
        ];

        $result = !empty($params['id']) ? $this->dao->update([
            'id' => $params['id']
        ], $data) : $this->dao->create($data);

        !$result && $this->error();
    }

    /**
     * 添加现金抵用券信息
     * @param array $params
     */
    public function addVoucher(array $params)
    {
        $user = UserJwtInstance::instance()->build()->getUser();

        $business = $user->business;

        !$business && $this->error('BUSINESS.NOT_EXIST');

        if( $business['status'] != 2 && $business['status'] != 1 ) {
            $this->error('BUSINESS.NOT_PASS_CERT');
        }

        // 优惠券开始时间 取时间戳
        $start_time  = strtotime($params['start_time']);

        // 固定截止日期 取时间戳
        $expire_time = strtotime($params['expire_time']);

        $data = [
            'business_id' => $business->id,
            'category_id' => 0,//暂时默认为0
            'type' => $this->dao::ENABLE_VOUCHER,//现金抵用券
            'title' => $params['title'],
            'goods_price' => $params['goods_price'],
            'reach_price' => $params['reach_price'],
            'price' => $params['price'],
            'stock' => $params['stock'],
            'status' => 0,
            'start_time' => $start_time,
            'expire_time' => $expire_time,
            'instructions' => $params['instructions'],
        ];

        $result = !empty($params['id']) ? $this->dao->update([
            'id' => $params['id']
        ], $data) : $this->dao->create($data);

        !$result && $this->error();
    }

    /**
     * 获取全店打折信息
     * @param int $type
     * @return array
     */
    public function getShopDiscount(int $type) {

        $user = UserJwtInstance::instance()->build()->getUser();

        $business = $user->business;

        if( $business['enable_full_discount'] != 1 ) {
            $this->error('BUSINESS.FULL_DISCOUNT_ERROR');
        }
        $status = 0;
        switch ( $type ) {
            case "1":
                $status = 0;
                break;
            case "2":
                $status = 1;
                break;
            case "3":
                $status = 2;
                break;
        }

        $shop_discount_list = $this->dao->getShopDiscount(['business_id' => $business->id, 'type' => $this->dao::SHOP_DISCOUNT, 'status' => $status], 'id desc');

        $result = [];
        foreach ($shop_discount_list as $key => $value) {
            $result[$key]['id'] = $value['id'];
            $result[$key]['type'] = $value['type'];
            $result[$key]['title'] = $value['title'];
            $result[$key]['picture'] = $value['picture'];
            $result[$key]['describe'] = $value['describe'];
            $result[$key]['status'] = $value['status'];
        }

        return $result;
    }

    /**
     * 获取优惠券列表
     * @param int $type
     * @return mixed
     */
    public function getCouponList(int $type)
    {
        $user = UserJwtInstance::instance()->build()->getUser();

        $business = $user->business;

        if( $business['enable_coupon'] != 1 ) {
            $this->error('BUSINESS.NOT_ALLOW_UPLOAD_COUPON');
        }

        $status = 0;
        switch ( $type ) {
            case "1":
                $status = 0;
                break;
            case "2":
                $status = 1;
                break;
            case "3":
                $status = 2;
                break;
        }

        // 获取商家优惠券列表
        $business_coupon_list = $this->dao->getCouponList(['business_id' => $business->id, 'type' => $this->dao::COUPON, 'status' => $status], 'id desc');

        $result = [];
        foreach ($business_coupon_list as $key => $value) {
            $result[$key]['id'] = $value['id'];
            $result[$key]['type'] = $value['type'];
            $result[$key]['title'] = $value['title'];
            $result[$key]['price'] = $value['price'];
            $result[$key]['goods_price'] = $value['goods_price'];
            $result[$key]['description'] = $value['description'];
            $result[$key]['start_time'] = $value['start_time'];
            $result[$key]['expire_time'] = $value['expire_time'];
            $result[$key]['instructions'] = $value['instructions'];
            $result[$key]['sales'] = $value['sales'];
            $result[$key]['stock'] = $value['stock'];
            $result[$key]['picture'] = $value['picture'];
            $result[$key]['describe'] = $value['describe'];
            $result[$key]['status'] = $value['status'];
        }

        return $result;
    }

    /**
     * 获取现金抵用券列表
     * @param int $type
     * @return mixed
     */
    public function getVoucherList(int $type)
    {
        $user = UserJwtInstance::instance()->build()->getUser();

        $business = $user->business;

        if( $business['enable_voucher'] != 1 ) {
            $this->error('BUSINESS.NOT_ALLOW_UPLOAD_VOUCHER');
        }

        $status = 0;
        switch ( $type ) {
            case "1":
                $status = 0;
                break;
            case "2":
                $status = 1;
                break;
            case "3":
                $status = 2;
                break;
        }

        // 获取现金抵用券列表
        $business_coupon_list = $this->dao->getCouponList(['business_id' => $business->id, 'type' => $this->dao::ENABLE_VOUCHER, 'status' => $status], 'id desc');

        $result = [];
        foreach ($business_coupon_list as $key => $value) {
            $result[$key]['id'] = $value['id'];
            $result[$key]['type'] = $value['type'];
            $result[$key]['title'] = $value['title'];
            $result[$key]['price'] = $value['price'];
            $result[$key]['goods_price'] = $value['goods_price'];
            $result[$key]['start_time'] = $value['start_time'];
            $result[$key]['expire_time'] = $value['expire_time'];
            $result[$key]['instructions'] = $value['instructions'];
            $result[$key]['sales'] = $value['sales'];
            $result[$key]['stock'] = $value['stock'];
            $result[$key]['reach_price'] = $value['reach_price'];
            $result[$key]['describe'] = $value['describe'];
            $result[$key]['status'] = $value['status'];
        }

        return $result;
    }

    /**
     * 删除优惠券
     *
     * @param array $ids
     */
    public function deleteCoupon(array $ids)
    {
        $user = UserJwtInstance::instance()->build()->getUser();
        $business = $user->business;

        $result = $this->dao->delete([
            'business_id' => $business->id,
            'id' => ['in', $ids],
        ]);

        if (!$result) $this->error();
    }

}